package com.wzh.activity.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wzh.activity.conf.ExceptionConf;
import com.wzh.activity.util.Md5Util;
import com.wzh.activity.vo.OrderVo;
import com.wzh.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.lettuce.core.RedisClient;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.activity.entity.OrderInfoEntity;
import com.wzh.activity.service.OrderInfoService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-02-22 09:21:45
 */
@RestController
@RequestMapping("activity/orderinfo")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null){
                params.put("userId",claims.getId());
                PageUtils page = orderInfoService.queryPage(params);
                return R.ok().put("page", page);
            }
            return R.error("用户未登录");
        }
        return R.error("用户未登录");
    }
    /**
     * 列表
     */
    @RequestMapping("/list2")
    public R list2(@RequestParam Map<String, Object> params){
           PageUtils page = orderInfoService.queryPage2(params);
           return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{goodsid}")
    public R info(@PathVariable("goodsid") Long goodsid,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null){
                OrderInfoEntity orderInfo = orderInfoService.getByGoodsIdAndUserId(goodsid,claims.getId());
                return R.ok().put("orderInfo", orderInfo);
            }
            return R.error("用户未登录");
        }
        return R.error("用户未登录");


    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save/{path}")
    //@SentinelResource(value = "post",blockHandler = "exceptionHandler",blockHandlerClass = {ExceptionConf.class})
    public R save(@PathVariable("path")String path, @RequestBody OrderVo orderVo, HttpServletRequest request) throws InterruptedException {
            redisTemplate.delete(orderVo.getGoodsId().toString());
            String token = request.getHeader("token");
            if (token!=null){
                Claims claims = jwtUtil.parseJWT(token);
                if (claims!=null){
                    String pathId = (String) redisTemplate.opsForValue().get(claims.getId()+"And"+orderVo.getActivityId());
                    if (path.equals(pathId)){
                        String code = (String) redisTemplate.opsForValue().get(orderVo.getFileName());
                        if (code!=null&&code.equals(orderVo.getCode())){
                            orderVo.setUserId(Long.valueOf(claims.getId()));
                            return orderInfoService.add(orderVo);
                        }else {
                         return R.error("验证码有误");
                        }
                    }
                    return R.error("非法访问，恶意刷单");
                }
                return R.error("用户未登录");
            }
            return R.error("用户未登录");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderInfoEntity orderInfo){
		orderInfoService.updateById(orderInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
